function searchBar(obj) {
	function cookie(name){
	   var cookieArray=document.cookie.split("; "); //得到分割的cookie名值对    
	   var cookie=new Object();    
	   for (var i=0;i<cookieArray.length;i++){    
	      var arr=cookieArray[i].split("=");       //将名和值分开    
	      if(arr[0]==name)return unescape(arr[1]); //如果是指定的cookie，则返回它的值    
	   } 
	   return ""; 
	} 
	function delCookie(name)//删除cookie
	{
	   document.cookie = name+"=;expires="+(new Date(0)).toGMTString();
	}
	function getCookie(objName){//获取指定名称的cookie的值
	    var arrStr = document.cookie.split("; ");
	    for(var i = 0;i < arrStr.length;i ++){
	        var temp = arrStr[i].split("=");
	        if(temp[0] == objName) return unescape(temp[1]);
	   }
	}
	function addCookie(objName,objValue,objHours){      //添加cookie

	    var str = objName + "=" + escape(objValue);

	    if(objHours > 0){                               //为时不设定过期时间，浏览器关闭时cookie自动消失

	        var date = new Date();

	        var ms = objHours*3600*1000;

	        date.setTime(date.getTime() + ms);

	        str += "; expires=" + date.toGMTString();

	   }

	   document.cookie = str;

	}

	 

	function SetCookie(name,value)//两个参数，一个是cookie的名子，一个是值

	{

	    var Days = 30; //此 cookie 将被保存 30 天

	    var exp = new Date();    //new Date("December 31, 9998");

	    exp.setTime(exp.getTime() + Days*24*60*60*1000);

	    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();

	}

	function getCookie(name)//取cookies函数        

	{

	    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));

	     if(arr != null) return unescape(arr[2]); return null;

	 

	}

	function delCookie(name)//删除cookie

	{

	    var exp = new Date();

	    exp.setTime(exp.getTime() - 1);

	    var cval=getCookie(name);

	    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();

	}


	var self = $(obj);
	var key = self.find('.k');
	var btn = self.find('.search_btn');
	log(btn);
	var rec = self.find('.search_record');
	var res = self.find('.search_result');
	var del = self.find('.u_btn_del');
	
	$(document).tap(function (ev) {
		var ev = ev || window.event;
		var target = ev.target || window.event;
		
		if (!$(target).parents('.search_bar').length) {
			rec.hide();
			res.hide();
		}
	});
	
	rec[0].style.top = res[0].style.top = self[0].offsetHeight + 'px';
	
	rec.on("touchend",clickList);
	res.on("touchend",clickList);
	
	// 点击列表
	function clickList(ev) {
		var ev = ev || window.event;
		var target = ev.target || ev.srcElement;
		var parent = null;
		
		if ($(target).hasClass('item')) parent = $(target);
		if ($(target).parents('.item').length) parent = $(target).parents('.item');

		if (parent) {
		var findtext=parent.find('.txt').text();
		//	key.val(parent.find('.txt').text());
			if(findtext.length>0)
			{
				Lib.href("so.aspx?word="+escape(findtext));
			}
			//alert('请求搜索结果');
		}
		
		// 点击清除历史记录
		if ($(target).hasClass('clear') || $(target).parents('.clear').length) {
			localStorage['damai.history'] = JSON.stringify([]);
		}
		
		drawDel();
		rec.hide();
		res.hide();
		ev.preventDefault(); 
	}
	
	// 点击搜索按钮
	btn.on("click",function () {
		if (!key.val().length) {
			key.focus();
			return;
		}
		
		var history = null;

		var t = JSON.parse(localStorage.getItem('damai.history'));
		history=t?t:null;
		
		if (!history) history = [];
		
		var isInsert = true;
		
		for (var i = 0, len = history.length; i < len; i++) {
			if (history[i] == key.val()) isInsert = false;
		}
		
		if (isInsert) history.push(key.val());
		
		localStorage['damai.history'] = JSON.stringify(history);
		Lib.href("so.aspx?word="+escape(key.val()));
		//alert('点击搜索，js/search.js源代码第65行');
		
		rec.hide();
		res.hide();
	});
	
	// 点击输入框
	key.tap(function () {
		if (!$(this).val().length) {
			loadHistory();
			rec.show();
			res.hide();
		} else {
			rec.hide();
			res.show();
		}
	});
	
	// 输入关键字
	key.on('input', function () {
		drawDel();
		
		if (!$(this).val().length) {
			rec.show();
			res.hide();
			
			// 无输入内容，请求历史记录
			loadHistory();
		} else {
			rec.hide();
			res.show();

			$.get("Data/LenWord.aspx",{word:$(this).val()},function(data){
				var ls="";for(var i in data){ls+='<a class="item"><span class="txt">'+data[i].ProjName+'</span><span class="num"></span>'}$(".search_layer").html(ls);
			},"json");
			// 有输入内容，请求搜索结果
			//alert('请求搜索提示，填充search_result列表数据，js/search.js源代码第96行');
		}
	});
	
	function drawDel() {
		if (!key.val().length) del.css({'display': 'none'});
		else del.css({'display': 'block'});
	}

	del.tap(function () {
		key.val('');
		rec.show();
		res.hide();
		drawDel();
		if (!key.val().length) rec.hide();
	});
	
	// 读取历史记录
	function loadHistory() {
		rec.html('');
		
		if (!localStorage['damai.history']) localStorage['damai.history'] = JSON.stringify([]);
		
		var history = JSON.parse(localStorage['damai.history']);
		var html = '';
		
		if (!history) history = [];
		
		for (var i = 0, len = history.length; i < len; i++) {
			html += '<a class="item"><span class="txt">' + history[i] + '</span></a>';
		}
		
		html += '<a class="clear"><span class="tc">清除历史搜索记录</span></a>';
		
		rec.html(html);
		
		if (!history.length) {
			rec.hide();
		}
	}
}